<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="isEdit ? '修改' : '新增'"
    width="900px"
    :visible.sync="visible"
  >
    <el-form :model="form" ref="form" :label-position="labelPosition" label-width="110px">
      <el-row :gutter="24">
        <el-col>
          <el-form-item label="入学日期">
            <el-date-picker
              type="date"
              :style="inputWidth"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              v-model="form.startDate"
              placeholder="请选择"
            />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="毕业日期">
            <el-date-picker
              type="date"
              :style="inputWidth"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              v-model="form.endDate"
              placeholder="请选择"
            />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="毕业学校">
            <el-input v-model="form.gradSchool" :style="inputWidth" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="专业">
            <el-input v-model="form.speciality" :style="inputWidth" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="学历">
            <el-select clearable :style="inputWidth" v-model="form.education" placeholder="请选择">
              <el-option
                v-for="(item,index) in eduArr"
                :key="index"
                :label="item.title"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="学位">
            <el-select clearable :style="inputWidth" v-model="form.degree" placeholder="请选择">
              <el-option
                v-for="(item,index) in speciaArr"
                :key="index"
                :label="item.title"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="学制">
            <el-input v-model="form.eduSystem" :style="inputWidth" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="学位证书编号">
            <el-input v-model="form.diplomaNo" :style="inputWidth" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="学历证书影印件">
            <el-input v-model="form.diplomaPhotocopy" :style="inputWidth" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="备注">
            <el-input type="textarea" :rows="2" v-model="form.remark" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button @click="handleUpdate" type="primary">{{ isEdit ? '修改' : '新增' }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateUserEdu, createUserEdu } from '@/api/archives'
import { openNotification, getSession, getDicData } from '@/utils/util'
export default {
  data() {
    return {
      visible: false,
      form: {},
      labelPosition: 'right',
      inputWidth: {
        width: '100%'
      },
      isEdit: false,
      eduArr: [],
      speciaArr: [],
      userRow: null
    }
  },
  created() {
    if (this.$route.query.userRow) {
      this.userRow = JSON.parse(this.$route.query.userRow)
    } else {
      this.userRow = getSession('userRow')
    }
  },
  methods: {
    handleUpdate() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          let res = null,
            mes = null
          this.$set(this.form, 'userId', this.userRow.userId)
          if (this.isEdit) {
            res = await updateUserEdu(this.form)
            mes = '修改操作成功'
          } else {
            res = await createUserEdu(this.form)
            mes = '新增操作成功'
          }
          if (res.state === '000000') {
            openNotification('success', mes)
            this.visible = false
            this.$emit('refresh')
          }
        }
      })
    },
    edit(bool, row) {
      console.log('row', row)
      this.visible = true
      this.isEdit = bool
      this.form = Object.assign({}, row)
    }
  },
  mounted() {
    this.eduArr = getDicData('001', '001011')
    this.speciaArr = getDicData('001', '001012')
  }
}
</script>
